package net.hnjdzy.tinydict.Fragment;


import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.squareup.picasso.Picasso;

import net.hnjdzy.tinydict.R;
import net.hnjdzy.tinydict.entity.Sentence;
import net.hnjdzy.tinydict.service.SentenceManager;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * A simple {@link Fragment} subclass.
 */
public class Sentenceragment extends Fragment {


    public Sentenceragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_sentenceragment, container, false);
           Button huoqu = view.findViewById(R.id.jt);
         final TextView time =view.findViewById(R.id.time);
        final Date date=new Date();
        final SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
        final Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
           huoqu.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   calendar .setTime(date);
                 ReadSentece readSentecce=new  ReadSentece();
                String datea=sf.format(calendar.getTime());
                readSentecce.execute(datea);
               }
           });
   Button ys=view.findViewById(R.id.ys);
   ys.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           calendar .add(Calendar.DAY_OF_MONTH,-1);
           ReadSentece readSentecce=new  ReadSentece();
           String datea=sf.format(calendar.getTime());
           readSentecce.execute(datea);
       }
   });

      Button nt=view.findViewById(R.id.nt);
      nt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        calendar .add(Calendar.DAY_OF_MONTH,+1);
        ReadSentece readSentecce=new  ReadSentece();
        String datea=sf.format(calendar.getTime());
        readSentecce.execute(datea);
    }
});
       String datea = sf.format(calendar.getTime());
        ReadSentece readSentece = new ReadSentece();
        readSentece.execute(datea);
        return  view;
    }


    class ReadSentece  extends AsyncTask<String,Void, Sentence> {
        @Override
        protected Sentence doInBackground(String... strings) {
             SentenceManager sentenceManager=   new SentenceManager();
            try {
                return  sentenceManager.getData(strings[0]);
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Sentence sentence) {
            String url=sentence.getPicture2();
          ImageView imageView= getView().findViewById(R.id.imageView);
          Picasso.with(getContext()).load(url).into(imageView);
          TextView ch= getView().findViewById(R.id.chanese);
          ch.setText(sentence.getNote());
          TextView eng=getView().findViewById(R.id.Englishsentence);
          eng.setText(sentence.getContent());
          TextView fr= getView().findViewById(R.id.frome);
          fr.setText(sentence.getTranslation());
            TextView time = getView().findViewById(R.id.time);
            time.setText(sentence.getDateline());

        }


    }



}

